package com.example.test;

import java.util.ArrayList;
import java.util.List;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import android.util.Log;

/**
 * @author lamtt
 * create: 07/02/2013
 * "Data access opject" class of Vocabulary object
 */
public class VocabularyDao {
	////////////////////////////////////////////////////////////////////////////
	// static
	////////////////////////////////////////////////////////////////////////////
	private Context mContext;
	private int mResponseCode=0;
	private static final String TITLE_ERROR_EXCEPTION = "LamTT --> Class: VocabularyDao - function: ";
	private static final String[] PROJECTION_ARRAY = { Vocabulary.ID, Vocabulary.KANJI, Vocabulary.JP_STRAN,Vocabulary.VN_STRAN,Vocabulary.SESSION };
	private static final Uri CONTENT_URI = Uri.parse("content://" + ConfigLib.PROVIDER_AUTHORITY + "/"+Vocabulary.TABLE_NAME );
	private static final String SORTBY = " " + Vocabulary.ID + " DESC ";
	private static final String LIMIT =" limit %s,%s ";
	private static final String QUERY_LITERAL = " = ? ";
	private static final int MAX_ITEM = 99;
	////////////////////////////////////////////////////////////////////////////
	// Contructor
	////////////////////////////////////////////////////////////////////////////
	
	/**
	 * @param context
	 */
	public VocabularyDao (Context context)
	 {
		 mContext=context;
	 }
	
	////////////////////////////////////////////////////////////////////////////
	// public Method
	////////////////////////////////////////////////////////////////////////////
	
	/**  insert Vocabulary table
	 * @param articleId
	 * @param createDate
	 *
	 */
	public boolean  insertVocabulary(VocabularyDto vocabularyDto) {
		ContentValues values;
		values = new ContentValues();
		values.put(Vocabulary.KANJI, vocabularyDto.kanji);
		values.put(Vocabulary.JP_STRAN, vocabularyDto.jp_stran);
		values.put(Vocabulary.VN_STRAN, vocabularyDto.vn_stran);
		Uri uri=mContext.getContentResolver().insert(CONTENT_URI, values);
		if(uri==null){
			return false;
		}
		return true;
	}
	/** delete row Vocabulary table
	 * @param articleId
	 * @param createDate
	 * @return long
	 */
	public long deleteVocabulary(String articleId) {
		String whereClause = Vocabulary.ID + QUERY_LITERAL;
		String[] whereArgs = new String[] {articleId.trim() };
		int result=mContext.getContentResolver().delete(CONTENT_URI, whereClause ,whereArgs);
		return result;
	}
	/** delete row Vocabulary table
	 * @param articleId
	 * @param createDate
	 * @return long
	 */
	public long deleteVocabularyByArrayId(String articleIdArr) {
		String whereClause = Vocabulary.ID + " In ("+articleIdArr+") ";
		int result= mContext.getContentResolver().delete(CONTENT_URI, whereClause ,null);
		return result;
	}
	/** delete row Vocabulary table
	 * @param articleId
	 * @param createDate
	 * @return long
	 */
	public long deleteAllVocabulary() {
		int result= mContext.getContentResolver().delete(CONTENT_URI, null ,null);
		return result;
	}
	
	/**
	 * @return row count
	 */
	public int getCountVocabularyRow()
	{
		int count=0;
		Cursor cursor = null;
	    try {
	    	cursor = mContext.getContentResolver().query(CONTENT_URI, PROJECTION_ARRAY, null, null, SORTBY);
	    	count = cursor.getCount();
		} catch (Exception e) {
			if (BuildConfig.DEBUG) {
	 			Log.e(TITLE_ERROR_EXCEPTION, "getCountVocabularyRow Exception");
	 		}
		}finally{
			if(cursor!=null){
				cursor.close();
			}
		}
	    return count;
	}
	/**get Vocabulary row by Id
	 * @param id
	 * @return VocabularyDto object
	 */
	public VocabularyDto getVocabularyById(long id) {
		
		String whereClause = Vocabulary.ID + QUERY_LITERAL;
		String[] whereArgs = new String[] {String.valueOf(id)};
	    Cursor cursor = mContext.getContentResolver().query(CONTENT_URI, PROJECTION_ARRAY, whereClause , whereArgs, SORTBY);
	    VocabularyDto VocabularyDto=null;
	    try {
	    	if(cursor.moveToFirst()){
	    		VocabularyDto=new VocabularyDto();
	    		VocabularyDto=getVocabularyDto(cursor);
	    	}
	    		
		} catch (Exception e) {
			if (BuildConfig.DEBUG) {
	 			Log.e(TITLE_ERROR_EXCEPTION, "getVocabularyById Exception");
	 		}
		}finally{
			cursor.close();
		}
	    return VocabularyDto;
	}
	/**get Vocabulary row By ArticleId
	 * @param articleId
	 * @return VocabularyCursor
	 */
	public VocabularyDto getVocabularyByArticleId(String articleId) {

		String whereClause = Vocabulary.ID + QUERY_LITERAL;
		String[] whereArgs = new String[] {articleId.trim() };
	    Cursor cursor = mContext.getContentResolver().query(CONTENT_URI, PROJECTION_ARRAY, whereClause, whereArgs, SORTBY);
	    cursor.getCount();
	    VocabularyDto VocabularyDto=null;
	    try {
	    	if(cursor.moveToFirst()){
	    		VocabularyDto=new VocabularyDto();
	    		VocabularyDto=getVocabularyDto(cursor);
	    	}
		} catch (Exception e) {
			if (BuildConfig.DEBUG) {
	 			Log.e(TITLE_ERROR_EXCEPTION, "getVocabularyByArticleId Exception");
	 		}
		}finally{
			cursor.close();
		}
	    return VocabularyDto;
	}

	/**get All Vocabulary
	 * @return VocabularyCursor
	 */
	public List<VocabularyDto> getListAllVocabulary() {
		return getListVocabulary(0,0);
	}
	/**get List Vocabulary
	 * @param articleId
	 * @return VocabularyCursor
	 */
	public List<VocabularyDto> getListVocabulary(int startNumber,int endNumber) {
		String whereClause = null;
		System.out.println("==============bo tay============"+startNumber + ":" +endNumber);
		StringBuilder condition=new StringBuilder(SORTBY)  ;
		if(startNumber >0 && endNumber > 0){
			whereClause = " " + Vocabulary.SESSION +" >= " +startNumber +" and "  +Vocabulary.SESSION +" <= " +endNumber; 
		}
	    Cursor cursor = mContext.getContentResolver().query(CONTENT_URI, PROJECTION_ARRAY, whereClause, null, condition.toString());
	    List<VocabularyDto> vocabularyListDto=null;
	    try {
	    	vocabularyListDto=new ArrayList<VocabularyDto>();
	    	vocabularyListDto= getListDtoFromCursor(cursor);
	    	System.out.println("==============bo tay============"+vocabularyListDto.size());
		} catch (Exception e) {
			if (BuildConfig.DEBUG) {
	 			Log.e(TITLE_ERROR_EXCEPTION, "getListVocabulary Exception");
	 		}
		}finally{
			cursor.close();
		}
	    return vocabularyListDto;
	}
	////////////////////////////////////////////////////////////////////////////
	// private Method
	////////////////////////////////////////////////////////////////////////////
	
	/**get List VocabularyDto From Cursor
	 * @param cursor
	 * @return List<VocabularyDto>
	 */
	private List<VocabularyDto> getListDtoFromCursor(Cursor cursor)
	{
		List<VocabularyDto> listVocabularyDto=new ArrayList<VocabularyDto>();
		if(cursor != null && cursor.getCount()>0){
			cursor.moveToFirst();
			do {
				listVocabularyDto.add(getVocabularyDto(cursor));
			} while (cursor.moveToNext());
		}
		return listVocabularyDto;
	}
	/**Add object from cursor
	 * @param cursor
	 * @return VocabularyDto
	 */
	private VocabularyDto getVocabularyDto(Cursor cursor)
	{

		VocabularyDto vocabularyDto =new VocabularyDto();
		vocabularyDto._id =cursor.getInt(0);
		vocabularyDto.kanji =cursor.getString(1);
		vocabularyDto.jp_stran =cursor.getString(2);
		vocabularyDto.vn_stran =cursor.getString(3);
		vocabularyDto.lession = cursor.getInt(4);
		return vocabularyDto;

	}
	
	
}
